 package munyorderweb;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import munyorderweb_DB.Change_Bean;
import munyorderweb_DB.DB_manager;

public class Price_total extends HttpServlet
{
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		order(req,resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		order(req,resp);
	}

	protected void order(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Connection con = null;

		try {
			System.out.println("DB1");
			con = DB_manager.getConnection();
			String sql = "select customer_time, sum(menu_price * order_count) as price from orders "
					+ "left join customer on customer.customer_no = orders.customer_no "
					+ "left join menu on menu.menu_no = orders.menu_no "
					+ "group by customer.customer_time";

			Statement stm = con.createStatement();
			ResultSet rs = stm.executeQuery(sql);

			ArrayList<Change_Bean> price_total = new ArrayList<Change_Bean>();

			Change_Bean change_bean = null;
			while ( rs.next() )
			{
				change_bean = new Change_Bean();

				change_bean.setCustomer_time(rs.getInt("customer_time"));
				change_bean.setPrice(rs.getInt("price"));

				price_total.add(change_bean);
			}
			req.setAttribute("price_total", price_total);

			RequestDispatcher dispatcher = req.getRequestDispatcher("Result_Price_total.jsp");
			dispatcher.forward(req, resp);
		}

		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				con.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
	}
}